import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

def three_sigma(ss):
    mean = ss.mean()  # 计算平均值
    std = ss.std()  # 计算标准差
    rule = (mean - 3 * std > ss) | (mean + 3 * std < ss)
    index = np.arange(ss.shape[0])[rule]  # 保存索引
    outliers = ss.iloc[index]  # 获取异常值
    return outliers, index


def getQL_QU(ss):
    quartiles = np.percentile(ss, [25, 50, 75])
    lower_quartile = quartiles[0]
    median = quartiles[1]
    upper_quartile = quartiles[2]

    iqr = upper_quartile - lower_quartile  # 四分位距
    upper = ss >= upper_quartile + 1.5 * iqr # 上限值
    lower = ss <= lower_quartile - 1.5 * iqr  # 下限值
    index = np.arange(ss.shape[0])[upper | lower]

    return index


data = pd.read_csv('students.csv')
outliers, index = three_sigma(data['age'])
print(outliers)
print(index)
ret = data.drop(index=index, inplace=False)
print(ret)


index = getQL_QU(data['age'])
ret = data.drop(index=index, inplace=False)
print(ret)

# data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
bp = data.boxplot(column = 'age', return_type='dict')
# print(bp)
#
# bp = plt.boxplot(data['age'])
fliers = [flier.get_ydata() for flier in bp['fliers']]
outliers = np.concatenate(fliers)
print("异常值：", outliers)

# ret = data.drop(index=index, inplace=False, axis=0)
# print(ret)

plt.show()


